/*************************************************
 * Filename: bet10.js
 * 这程式最後会送出，范例如以下值
 * [type] => 2~5            //下注类别
 * [num1] => 7,19,31,43     //号码组合，如果是连碰状态就只有num1
 * [num2] => 4,14,24,34,44  //号码组合
 * [betUnionNum] => 20      //组合单数
 *************************************************/
/**属性*******************************************/
var defaultBetType = 0;    //预设下注类别为 无
var defaultMultiType = 0;  //预设对碰类别为连碰

var currentBetType = defaultBetType;      //目前的下注类别
var currentMultiType = defaultMultiType;  //目前的对碰类别

var chosenNum = [];       //已选择的号码，如果是胆托则只存非胆拖的值
var chosenMultiNum = [];  //已选择的胆托号码

var chosenNumAnimals2 = [];      //已选择的号码，如果是胆托则只存非胆拖的值
var chosenNumAnimals1 = [];      //已选择的胆托号码

var betUnion = [];                 //下注组合
var betUnionLength = new Number(); //下注组合数，单数

var TwelveAnimals = ['','鼠','牛','虎','兔','龙','蛇','马','羊','猴','鸡','狗','猪']; //用来储存生肖字串，索引0空出来，从1开始

/*******************************************属性**/
/**ctrl 选项控制*********************************/
//下注类别  Todo清空演算後展开的资料
function ctrlBetType(ctrlVal){
    ctrlVal = parseInt(ctrlVal);
    switch(ctrlVal){
        case 2:
        case 3:
        case 4:
        case 5:
            switchBlockDisplay("notInThreeStar", 0);//关闭生肖、尾数
    }
    if(currentMultiType == 0 || currentMultiType == 1){
        clearChecked('ballNum');//避免0、1之前切换前有留资料，先清除一次
    }
    currentBetType = ctrlVal;//目前的下注类别
    clearRecord();//清除纪录
}

//对碰类别  Todo清空演算後展开的资料
function ctrlMultiType(ctrlVal){
	ctrlVal = parseInt(ctrlVal);
    switch(ctrlVal){
        case 0://连碰
        case 1://胆拖
            switchBlockDisplay("numBlock", 1);//开启号码区块
            clearChecked('ballNum');//避免0、1之前切换前有留资料，先清除一次
        break;
    }
    currentMultiType = ctrlVal;//目前的对碰类别
    clearRecord();//清除纪录
}

//号码
function ctrlNum(ctrl){
    var ctrlValue = parseInt(ctrl.value);
    if(currentMultiType == 0){//连碰
        if(ctrl.checked == true){//选取
            chosenNum.push(ctrlValue);//新增
			chosenNumAnimals2.push(TwelveAnimals[ctrlValue]);
        }else{//选消
            chosenNum = _arrayRemove(chosenNum, ctrlValue);
			chosenNumAnimals2 = _arrayRemove(chosenNumAnimals2, TwelveAnimals[ctrlValue]);
        }
        chosenNum = _arraySort(chosenNum);//已选择的号码重新排列
        computeUnion();//计算组合
    }else if(currentMultiType == 1){//胆拖
		if(currentBetType == 2){//1胆
			if(chosenMultiNum.length < 1){
				chosenMultiNum.push(ctrlValue);//新增
				chosenNumAnimals1.push(TwelveAnimals[ctrlValue]);
				ctrl.disabled = true;
				displayMultiMap();//输出胆状态
			}else{
				if(ctrl.checked == true){//选取
					chosenNum.push(ctrlValue);//新增
					chosenNumAnimals2.push(TwelveAnimals[ctrlValue]);
				}else{//选消
					chosenNum = _arrayRemove(chosenNum, ctrlValue);//移除
					chosenNumAnimals2 = _arrayRemove(chosenNumAnimals2, TwelveAnimals[ctrlValue]);
				}
			}
		}else if(currentBetType == 3){//2胆
			if(chosenMultiNum.length < 2){
				chosenMultiNum.push(ctrlValue);//新增
				chosenNumAnimals1.push(TwelveAnimals[ctrlValue]);
				ctrl.disabled = true;
				displayMultiMap();//输出胆状态
			}else{
				if(ctrl.checked == true){//选取
					chosenNum.push(ctrlValue);//新增
					chosenNumAnimals2.push(TwelveAnimals[ctrlValue]);
				}else{//选消
					chosenNum = _arrayRemove(chosenNum, ctrlValue);//移除
					chosenNumAnimals2 = _arrayRemove(chosenNumAnimals2, TwelveAnimals[ctrlValue]);
				}
			}
			chosenMultiNum = _arraySort(chosenMultiNum);//重新排列
		}else if(currentBetType == 4){//3胆
			if(chosenMultiNum.length < 3){
				chosenMultiNum.push(ctrlValue);//新增
				chosenNumAnimals1.push(TwelveAnimals[ctrlValue]);
				ctrl.disabled = true;
				displayMultiMap();//输出胆状态
			}else{
				if(ctrl.checked == true){//选取
					chosenNum.push(ctrlValue);//新增
					chosenNumAnimals2.push(TwelveAnimals[ctrlValue]);
				}else{//选消
					chosenNum = _arrayRemove(chosenNum, ctrlValue);//移除
					chosenNumAnimals2 = _arrayRemove(chosenNumAnimals2, TwelveAnimals[ctrlValue]);
				}
			}
			chosenMultiNum = _arraySort(chosenMultiNum);//重新排列
		}else if(currentBetType == 5){//4胆
			if(chosenMultiNum.length < 4){
				chosenMultiNum.push(ctrlValue);//新增
				chosenNumAnimals1.push(TwelveAnimals[ctrlValue]);
				ctrl.disabled = true;
				displayMultiMap();//输出胆状态
			}else{
				if(ctrl.checked == true){//选取
					chosenNum.push(ctrlValue);//新增
					chosenNumAnimals2.push(TwelveAnimals[ctrlValue]);
				}else{//选消
					chosenNum = _arrayRemove(chosenNum, ctrlValue);//移除
					chosenNumAnimals2 = _arrayRemove(chosenNumAnimals2, TwelveAnimals[ctrlValue]);
				}
			}
			chosenMultiNum = _arraySort(chosenMultiNum);//重新排列
		}
		chosenNum = _arraySort(chosenNum);//已选择的号码重新排列
		computeUnion();//计算组合
    }

}

//送出前检查表单
function chkForm(frm){
    //检查下注栏位
	if (currentBetType<2 || currentBetType>5) {
		alert('请先选择类别');
		return false;
	}
	switch(currentMultiType){
		case 0://连碰，To Do检查还可以再细一点
			if(chosenNum < 1){
				alert('生肖未选择');
				return false;
			}
		break;
        
		case 1://胆拖，To Do检查还可以再细一点
			if(chosenNum < 1){
				alert('生肖未选择');
				return false;
			}
		break;
	}
    frm.submit();

}
/*********************************ctrl 选项控制**/


/**显示及作用控制*********************************/
//区块显示控制
function switchBlockDisplay(blockId, isDisplay){
    var targetBlock = document.getElementById(blockId);
    if(isDisplay == 1){
        if(targetBlock.style.display != "block") targetBlock.style.display = "block";
        //enable控制项
    }else{
        if(targetBlock.style.display != "none") targetBlock.style.display = "none";
        //disable控制项
		switch(blockId){
            case "numBlock"://号码区块
                clearChecked('ballNum');
            break;
        }
    }
}
//显示胆拖状态
function displayMultiMap(){
    if(chosenMultiNum.length > 0){
        for(var i=0; i < chosenMultiNum.length;i++){
            var targetCaption = document.getElementById('multiMapCap['+i+']');
            var targetValue = document.getElementById('multiMapVal['+i+']');
            targetCaption.innerHTML =(chosenMultiNum.length > 1)? '胆'+ (i+1) +':' : '胆';
            targetValue.innerHTML = chosenNumAnimals1[i];
        }
    }
}
//清除显示胆拖状态
function clearDisplayMultiMap(){
    for(var i=0; i < 4;i++){
        var targetCaption = document.getElementById('multiMapCap['+i+']');
        var targetValue = document.getElementById('multiMapVal['+i+']');
        targetCaption.innerHTML = '&nbsp;';
        targetValue.innerHTML = '&nbsp;';
    }

}
//显示下注状态讯息
function displayBetStatus(statusMessage){
    document.getElementById("betStatus").innerHTML = statusMessage;
}

//清除 checked group
function clearChecked(groupName){
    var targetGroup = document.forms["betForm"][groupName];
    for (var i = 0; i < targetGroup.length; i++){
        if (targetGroup[i].checked == true){
            targetGroup[i].checked = false;
        }
        if (targetGroup[i].disabled == true){
            targetGroup[i].disabled = false;
        }
    }
}

//清除纪录
function clearRecord(){
    chosenNum = [];//已选择的号码，如果是胆托则只存非胆拖的值
    chosenMultiNum = [];//已选择的胆托号码
    chosenNumAnimals2 = [];      //已选择的号码，如果是胆托则只存非胆拖的值
    chosenNumAnimals1 = [];      //已选择的胆托号码
    chosenMixMap = [Number.NaN, Number.NaN, Number.NaN, Number.NaN];
    betUnion = [];//下注组合
    betUnionLength = [];//下注组合数
    clearBetUnionValue();//清除组合隐藏栏位值
    displayBetStatus('');//清除下注状态讯息
    clearDisplayMultiMap();//清除显示胆拖状态
}
//清除表单
function clearForm(){
    //依目前所处状态清除内容，包含下注金额也须删除
    switch(currentMultiType){
        case 0://号码区块
        case 1:
            clearChecked('ballNum');
        break;
    }
    // document.getElementById("money").value = "";
    clearRecord();
}
/*********************************显示及作用控制**/

/**产生组合***************************************/
//计算组合 
function computeUnion(){
    switch(currentMultiType){
        case 0://连碰
            betUnion = chosenNum;//下注组合取值
			betUnionLength = chosenNum.length;//组合数
			if (currentBetType == 2) {  // 2支号码一组
				var bet_Length = betUnionLength*(betUnionLength-1)/2;
			} else if (currentBetType == 3) {  // 3支号码一组
				var bet_Length = betUnionLength*(betUnionLength-1)*(betUnionLength-2)/6;//组合数
			} else if (currentBetType == 4) {  // 4支号码一组
				var bet_Length = betUnionLength*(betUnionLength-1)*(betUnionLength-2)*(betUnionLength-3)/24;//组合数
			} else {
				var bet_Length = betUnionLength*(betUnionLength-1)*(betUnionLength-2)*(betUnionLength-3)*(betUnionLength-4)/120;//组合数
			}
            createBetUnionValue(betUnion, 1, false);//写入隐藏栏位
            //显示状态
            var statusMessage = "[" + chosenNumAnimals2.join(",") + "]，组成"+ bet_Length +"笔";
            displayBetStatus(statusMessage);
        break;
        case 1://胆拖
			//胆固定
			betUnionLength = chosenNum.length;//组合数
			betUnion = new Array(2);//初始化为长度2的阵列
			betUnion[0] = chosenMultiNum;
			betUnion[1] = chosenNum;
			createBetUnionValue(betUnion, betUnionLength, true);//写入隐藏栏位
			//显示状态
			var statusMessage = "[" + chosenNumAnimals1.join(",") + "]";
			if(betUnionLength > 0){
				statusMessage += " > [" + chosenNumAnimals2.join(",") + "]";
				statusMessage += "，组成"+ betUnionLength +"笔";
			}
			displayBetStatus(statusMessage);
        break;
    }
}

//赋予组合隐藏栏位值
function createBetUnionValue(valueArray, betNum, isMultiDimen){
    var targetBlock = document.getElementById("betUnionBlock");
    //先清空
    clearBetUnionValue();
    //开始写入组合
    if(isMultiDimen == false){ //一维阵列
        betNum = 1;
        var hiddenItem = document.createElement("input");
        hiddenItem.setAttribute("id", 'num1');
        hiddenItem.setAttribute("name", 'num1');
        hiddenItem.setAttribute("type","hidden");
        hiddenItem.setAttribute("value", valueArray.join(","));
        targetBlock.appendChild(hiddenItem);
    }else{
        //写入各选项的组合
        for(var i=0;i < valueArray.length; i++){
            var hiddenItem = document.createElement("input");
            hiddenItem.setAttribute("id", 'num'+(i+1));
            hiddenItem.setAttribute("name", 'num'+(i+1));
            hiddenItem.setAttribute("type","hidden");
            hiddenItem.setAttribute("value", valueArray[i].join(","));
            targetBlock.appendChild(hiddenItem);
        } 
    }
    //下注组合数，单数
    var hiddenBetUnionNum = document.createElement("input");
    hiddenBetUnionNum.setAttribute("id", 'betUnionNum');
    hiddenBetUnionNum.setAttribute("name", 'betUnionNum');
    hiddenBetUnionNum.setAttribute("type","hidden");
    hiddenBetUnionNum.setAttribute("value", betNum);
    targetBlock.appendChild(hiddenBetUnionNum);
}

//清除组合隐藏栏位值
function clearBetUnionValue(){
    var targetBlock = document.getElementById("betUnionBlock");
    targetBlock.innerHTML ='';
}
/***************************************产生组合**/
/**工具函式***************************************/
//初始化属性
//取得选项的值
function _getRadioValueByName(groupName){
    var targetGroup = document.forms["betForm"][groupName];
    var returnValue = new Number(); 
    for (var i = 0; i < targetGroup.length; i++){
        if (targetGroup[i].checked == true){
            returnValue = targetGroup[i].value;
        }
    }
    return returnValue;
}
//阵列排序
function _arraySort(sortArray){
    arrayLength = sortArray.length;
    for (var i = 0; i < arrayLength; i++) {
        var arrayValue = sortArray[i];
        for (var j = i - 1; ( (j >= 0) && (sortArray[j] > arrayValue) ); j--) {
            sortArray[j + 1] = sortArray[j];
        }
        sortArray[j + 1] = arrayValue;
    }
    return sortArray;
}

//阵列remove
function _arrayRemove(removeArray, removeKey){
    var tmpArray = new Array();
    for(var i=0; i < removeArray.length; i ++){
        if(removeArray[i] != removeKey){
            tmpArray.push(removeArray[i]);
        }
    }
    return tmpArray;
}

/***************************************工具函式**/
function nowdate()
{
    document.all.hktime.innerHTML = "现在 <b>"+now.getDate()+"</b> 日 <b>"+d2(now.getHours())+" : "+d2(now.getMinutes())+" : "+d2(now.getSeconds())+"</b>";
    now.setTime(now.getTime() + 1000);
    setTimeout("nowdate()",1000);
}

function countdown()
{
  timediff = timestap;
  days = Math.floor(timediff / (60 * 60 * 24));
	timediff -= days * (60 * 60 * 24);
	hours = Math.floor(timediff / (60 * 60));
	timediff -= hours * (60 * 60);
	mins = Math.floor(timediff / (60));
	timediff -= mins * (60);
	secs = Math.floor(timediff);

    str = "离截止下注 ";
    if (days > 0 ) str += "<b>" + days + "</b> 天 ";
    if (hours > 0) str += "<b>" + hours + "</b> 时 ";  // 
    if (mins > 0) str += "<b>" + d2(mins) + "</b> 分 ";  // 
    str += "<b>" + d2(secs) + "</b> 秒"; //
    countdownstr.innerHTML =  str;
    setTimeout("countdown()",1000)
    timestap-=1;
    if (timestap==0) {
        location.reload();
    }
}
function d2(v)
{
	return (v<=9?'0':'')+v;	
}